﻿using UnityEngine;
using UnityEngine.UI;

namespace UnityView
{
    public class ImageView : UIView
    {
        public Image.Type ImageType
        {
            get { return ImageComponent.type; }
            set { ImageComponent.type = value; }
        }

        public ImageView(string name = "Image View") : this(UICanvas.GetInstance(), name)
        {
        }

        public ImageView(IViewLayout view, string name = "Image View") : base(view, name)
        {
            imageComponent = UIObject.AddComponent<Image>();
        }
    }

    public class RawImageView : UILayout
    {
        public CanvasRenderer CanvasRenderer { get; set; }

        public readonly RawImage RawImage;

        public bool IsRaycastTarget
        {
            get => RawImage.raycastTarget;
            set => RawImage.raycastTarget = value;
        }

        public Texture Texture
        {
            set { RawImage.texture = value; }
            get { return RawImage.texture; }
        }

        public Material Material
        {
            set { RawImage.material = value; }
            get { return RawImage.material; }
        }

        private const string DefaultRawImageViewName = "Raw Image View";

        public RawImageView(string name = DefaultRawImageViewName) : this(UICanvas.GetInstance(), name)
        {
        }

        public RawImageView(IViewLayout view, string name = DefaultRawImageViewName) : base(view, name)
        {
            CanvasRenderer = UIObject.AddComponent<CanvasRenderer>();
            RawImage = UIObject.AddComponent<RawImage>();
        }

        public RawImageView(GameObject gameObject) : base(gameObject)
        {
            CanvasRenderer = gameObject.GetComponent<CanvasRenderer>() ?? gameObject.AddComponent<CanvasRenderer>();
            RawImage = gameObject.GetComponent<RawImage>() ?? gameObject.AddComponent<RawImage>();
        }
    }
}